package com.zaizai.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zaizai.reggie.common.R;
import com.zaizai.reggie.dto.DishDto;
import com.zaizai.reggie.entity.Category;
import com.zaizai.reggie.entity.Dish;
import com.zaizai.reggie.entity.DishFlavor;
import com.zaizai.reggie.service.CategoryService;
import com.zaizai.reggie.service.DishFlavorService;
import com.zaizai.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name) {
        //构造分页构造器
        Page<Dish> pageInfo=new Page(page,pageSize);
        Page<DishDto> dishDtoPagepageInfo=new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        //执行查询
        dishService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPagepageInfo,"records");
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list=records.stream().map((item)->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                dishDto.setCategoryName(category.getName());
            }
            return dishDto;

        }).collect(Collectors.toList());
        dishDtoPagepageInfo.setRecords(list);
        return R.success(dishDtoPagepageInfo);

    }
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        DishDto withFlavor = dishService.getWithFlavor(id);

        if(withFlavor!=null){
            return R.success(withFlavor);

        }
        return R.error("查找失败");

    }

    @GetMapping("/list")
    public R<List<Dish>> getByType(Dish dish) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        List<Dish> dishes = dishService.list(lambdaQueryWrapper);
        List<DishDto> dishDtos=dishes.stream().map((item)->{
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(dishes,dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                dishDto.setCategoryName(category.getName());
            }
            Long itemId = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(DishFlavor::getDishId,itemId);
            List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper1);
            dishDto.setFlavors(list);
            return dishDto;

        }).collect(Collectors.toList());




        return R.success(dishes);






    }



    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());

        dishService.updateWithFlavor(dishDto);

        return R.success("新增菜品成功");
    }




}
